package com.epoint.androidphone.mobileoa.ui.todostandard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.androidmobile.bizlogic.mail.task.Mail_GetList_Task;
import com.epoint.androidmobile.bizlogic.todo.model.HandleListModel;
import com.epoint.androidmobile.bizlogic.todo.task.Get_Handle_GetUnHandleList_Task;
import com.epoint.androidmobile.bizlogic.todo.task.Handle_GetHandleList_YiBan_Task;
import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidmobile.core.xml.XMLUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;

/**
 * @author liyc
 * @time 2013-5-10 下午3:55:58
 * @annomation 标准工作流
 */
public class TodoStandardListView extends SuperPhonePage {
	int pageindex = 1;
	int pagesize = 20;
	ListView lv;
	List<HandleListModel> list = new ArrayList<HandleListModel>();
	long getTodoListTaskID;
	LstAdapter adapter = new LstAdapter();
	ImageView ivQuery;
	EditText etCondition;
	int todoFlag = 0;// 1 已办 0 待办

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.todolistview, getIntentViewTitle());
		ivQuery = (ImageView) findViewById(R.id.ivQuery);
		ivQuery.setOnClickListener(this);
		etCondition = (EditText) findViewById(R.id.etCondition);
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);

		getBtRight().setText("操作");
		getBtRight().setVisibility(View.VISIBLE);

		addFoot();
		lv.setAdapter(adapter);

		getData(pageindex, pagesize, "", "");
	}

	public void getData(int _pageindex, int _pagesize, String _keyword,
			String _handletype) {
		if (todoFlag == 1) {
			getYbData(_pageindex, _pagesize, _keyword, _handletype);
			return;
		}
		showTopProgressBar();
		HashMap<String, Object> param = getPassMap();
		param.put("KeyWord", _keyword);
		param.put("CurrentPageIndex", _pageindex + "");
		param.put("PageSize", _pagesize + "");
		param.put("HandleType", _handletype);
		getTodoListTaskID = new Get_Handle_GetUnHandleList_Task(this, param)
				.startTask();
	}

	public void getYbData(int _pageindex, int _pagesize, String _keyword,
			String _handletype) {
		showTopProgressBar();
		HashMap<String, Object> param = getPassMap();
		param.put("KeyWord", _keyword);
		param.put("CurrentPageIndex", _pageindex + "");
		param.put("PageSize", _pagesize + "");
		param.put("HandleType", _handletype);
		getTodoListTaskID = new Handle_GetHandleList_YiBan_Task(this, param)
				.startTask();
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);

		if (v == ivQuery) {
			list.clear();
			pageindex = 1;
			if (lv.getFooterViewsCount() == 0) {
				addFoot();
			}
			lv.setVisibility(View.INVISIBLE);
			getData(pageindex, pagesize, etCondition.getText().toString(), "");
		} else if (v == getBtRight()) {
			AlertUtil.showAlertMenu(this, "查询",
					new String[] { "已办事宜", "待办事宜" },
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							list.clear();
							if (which == 0) {
								todoFlag = 1;
								getTvTitle().setText("已办事宜");
							} else if (which == 1) {
								todoFlag = 0;
								getTvTitle().setText("待办事宜");
							}
							pageindex = 1;
							getData(pageindex, pagesize, "", "");
						}
					});
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		super.onItemClick(parent, view, position, id);
		if (position == list.size()) {
			pageindex++;
			getData(pageindex, pagesize, etCondition.getText().toString(), "");
			return;
		} else {
			if (1 != todoFlag
					&& list.get(position).CanHanle.equalsIgnoreCase("0")) {
				ToastUtil.toastWorning(this, "该待办请在PC上处理！");
				return;
			}
			Intent intent = new Intent(this, TodoStandardDetailView.class);
			intent.putExtra("MessageItemGuid",
					list.get(position).MessageItemGuid);
			intent.putExtra("FileType", list.get(position).FileType);
			intent.putExtra("todoFlag", String.valueOf(todoFlag));
			startActivity(intent);
		}
	}

	private LinearLayout foot;

	public void addFoot() {
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT, 55));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if (taskId == getTodoListTaskID) {
			lv.setVisibility(View.VISIBLE);
			if (validateXML(obj)) {
				String bs = obj.toString();
				String listxml = StringHelp.getXMLAttOut(bs, "HandleList");
				List<HandleListModel> listtemp = XMLUtil.DomAnalysisCommon(
						listxml, HandleListModel.class);
				for (HandleListModel a : listtemp) {
					list.add(a);
				}
				adapter.notifyDataSetChanged();

				if (listtemp.size() < pagesize) {
					lv.removeFooterView(foot);
				}
				adapter.notifyDataSetChanged();
			} else {
				finish();
			}
		}
	}

	class LstAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			LayoutInflater mInflater = LayoutInflater.from(getContext());
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(
						R.layout.writesigntodolistlayout, null);
				holder.tvTitle = (TextView) convertView
						.findViewById(R.id.tvTitle);
				holder.tvSender = (TextView) convertView
						.findViewById(R.id.tvSender);
				holder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTime);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			HandleListModel a = list.get(position);

			holder.tvTitle.setText(a.Title);
			holder.tvSender.setText(a.FromDispName);
			holder.tvTime.setText(a.GenerateDate);
			return convertView;
		}

		public class ViewHolder {
			public TextView tvTitle;
			public TextView tvSender;
			public TextView tvTime;
		}
	}

}
